package cn.stylefeng.guns.modular.business.service.impl;

import cn.stylefeng.guns.config.redis.redis.util.Redishandler0;
import cn.stylefeng.guns.modular.business.common.Constants;
import cn.stylefeng.guns.modular.business.common.GunsData;
import cn.stylefeng.guns.modular.business.entity.MessageNotice;
import cn.stylefeng.guns.modular.business.mapper.MessageNoticeMapper;
import cn.stylefeng.guns.modular.business.model.result.IndexResult;
import cn.stylefeng.guns.modular.business.model.result.MessageNumResult;
import cn.stylefeng.guns.modular.business.service.MessageNoticeService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MessageNoticeServiceImpl extends ServiceImpl<MessageNoticeMapper, MessageNotice> implements MessageNoticeService {

    @Autowired
    MessageNoticeMapper messageNoticeMapper;

    @Autowired
    Redishandler0 redishandler0;

    @Override
    public List<MessageNotice> listByUserId(Page page, String condition, String type, Long userId) {
        return messageNoticeMapper.listByUserId(page, condition, type, userId);
    }

    @Override
    public List<MessageNotice> list(Page page, String condition, String type) {
        return messageNoticeMapper.list(page, condition, type);
    }

    /**
     * 新增消息
     *
     * @param title
     * @param content
     * @param type
     * @return
     */
    @Override
    public boolean add(String title, String content, String type, Long userId) {
        MessageNotice messageNotice = new MessageNotice();
        messageNotice.setUserId(userId);
        messageNotice.setTitle(title);
        messageNotice.setContent(content);
        messageNotice.setType(type);
        messageNotice.setCreateTime(new Date());
        messageNotice.setUpdateTime(new Date());
        this.save(messageNotice);
        //按不同类型缓存用户消息0-公告，1-系统消息，2-平台通知
        if (type.equals("0")) {
            IndexResult indexResult = GunsData.indexMap.get(Constants.INDEX_MAP_KEY);
            if (indexResult.getMessageNoticeResult() != null) {
                indexResult.setMessageNoticeResult(null);
            }
            redishandler0.cacheZSet(Constants.NOTICE_KEY, "", JSONObject.toJSONString(messageNotice), Double.valueOf(JSONArray.toJSONString(messageNotice.getCreateTime())));
        } else if (type.equals("1")) {
            redishandler0.cacheZSet(Constants.SYSTEM_INFO_KEY, userId.toString(), JSONObject.toJSONString(messageNotice), Double.valueOf(JSONArray.toJSONString(messageNotice.getCreateTime())));
            redishandler0.incr(Constants.UNREAD_MESSAGE_COUNT_KEY, userId.toString() + 1, 1);
        } else if (type.equals("2")) {
            redishandler0.cacheZSet(Constants.PLATFORM_NOTICE_KEY, "", JSONObject.toJSONString(messageNotice), Double.valueOf(JSONArray.toJSONString(messageNotice.getCreateTime())));
            redishandler0.incr(Constants.UNREAD_MESSAGE_COUNT_KEY, userId.toString() + 2, 1);
        }

        return true;
    }


}
